import { Component, OnInit, ViewChild } from '@angular/core';
import { ActivatedRoute, Params, Router } from '@angular/router';
import { HttpService } from '../../service/http.service';
import { PTRComponent, ToptipsComponent } from 'ngx-weui';
import { Observable } from 'rxjs/Observable';

@Component({
  selector: 'app-deptlist',
  templateUrl: './deptlist.component.html',
  styleUrls: ['./deptlist.component.css']
})

export class DeptlistComponent implements OnInit {

  regtype: number;
  deptlisturl = 'hospital!getDepartmentListById?h_id=';
  hospindexurl = 'hospital!getHospitalInfoById?h_id=';
  hospid: any;
  deptlist: any;
  hospindex: any;
  type = 'loading';
  first = false;
  
  @ViewChild('toptips') toptips: ToptipsComponent;
  constructor(private routerdata: ActivatedRoute, private listdata: HttpService,
    private router: Router) {

  }

  ngOnInit() {
    this.routerdata.params.subscribe((params: Params) => { this.hospid = params['id'], this.regtype = params['regtype']; });
    this.getDeptData();
    this.getHospindexData();
    setTimeout(() => this.first = true, 300);
  }
  //获取医院排班数据
  getDeptData() {
    this.listdata.httpGet(this.deptlisturl+this.hospid, {}).subscribe(res => {
      this.deptlist = res[0];
      console.log('接口名称:'+this.deptlisturl, this.deptlist);
    });
  }
  //获取医院数据
  getHospindexData() {
    this.listdata.httpGet(this.hospindexurl + this.hospid, {}).subscribe(res => {
      this.hospindex = res[0];
      console.log('接口名称:'+this.hospindexurl, this.hospindex);
    });
  }

  /*顶部提示*/
  onShow(type: 'warn' | 'info' | 'primary') {
    this.toptips.type = type;
    switch (type) {
      case 'warn':
        this.toptips.text = '刷新失败！';
        break;
      case 'primary':
        this.toptips.text = '刷新成功！';
        break;
      case 'info':
        this.toptips.text = ' Thanks for coming! ';
        break;
    }
    this.toptips.onShow();
  }

  /*配合刷新组件方法*/
  onRefresh(ptr: PTRComponent) {
    Observable.timer(800).subscribe(() => {
      this.ngOnInit();
      this.onShow('primary');
      ptr.setFinished('上次刷新：' + new Date().getHours() + ':' + new Date().getMinutes() + ':' + new Date().getSeconds());
    });
  }
}
